﻿using System;
using MvvmLight;
using MvvmLight.Command;
using vDisk.WindowsPhone;
using Microsoft.Phone.Scheduler;
using TechNewsReader.Common;

namespace TechNewsReader.ViewModel
{
    public class SettingViewModel : ViewModelBase
    {
        #region Property

        private bool _isTaskEnabled;
        public bool IsTaskEnabled
        {
            get
            {
                return _isTaskEnabled;
            }
            set
            {
                _isTaskEnabled = value;
                if (value)
                {
                    StartTask(ResourcesConstants.PeriodTaskName);
                }
                else
                {
                    StopTask(ResourcesConstants.PeriodTaskName);
                }
                RaisePropertyChanged(() => IsTaskEnabled);
            }
        }

        public SettingViewModel()
        {

        }

        #endregion

        #region Constructor


        #endregion

        #region Command
        private RelayCommand _uploadCommand;
        public RelayCommand UploadCommand
        {
            get
            {
                return _uploadCommand ?? (_uploadCommand = new RelayCommand(() =>
                    {
                        UploadLog();
                    }));
            }
        }


        #endregion

        #region method
        private void UploadLog()
        {
            NetService net = new NetService();
            net.ServicecallBack = UploadCallback;
            net.Upload(new UploadFileReq { });
        }

        private void UploadCallback(bool isSuccess, object response, string errmsg)
        {

        }

        private void StartTask(string periodicTaskName)
        {
            var periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;
            if (periodicTask != null && periodicTask.IsEnabled)
            {
                periodicTask.ExpirationTime = DateTime.Now.AddDays(14);
            }
            else
            {
                try
                {
                    periodicTask = new PeriodicTask(periodicTaskName);
                    {
                        periodicTask.ExpirationTime = DateTime.Now.AddDays(14);
                    }
                    periodicTask.Description = "Just a test";
                    ScheduledActionService.Add(periodicTask);
                }
                catch (Exception ex)
                {
                    //Logger.Warn(ex.Message);
                }
            }
#if DEBUG
            ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromMilliseconds(10));
#endif
        }

        private void StopTask(string periodicTaskName)
        {
            try
            {
                if (ScheduledActionService.Find(periodicTaskName) != null)
                    ScheduledActionService.Remove(periodicTaskName);
            }
            catch (Exception ex)
            {
                //Logger.Warn(ex.Message);
            }
        }

        #endregion
    }
}
